package com.cqs.leetcode;

import com.cqs.leetcode.ds.ArraysUtils;

public class FlippingAnImage832 {

    public int[][] flipAndInvertImage(int[][] A) {
        for (int i = 0; i < A.length; ++i) {
            reverse(A[i]);
            invert(A[i]);
        }
        return A;
    }


    private void reverse(int[] a) {
        int left = 0;
        int right = a.length - 1;
        while (left < right) {
            swap(a, left++, right--);
        }
    }

    private void swap(int[] a, int left, int right) {
        int tmp = a[left];
        a[left] = a[right];
        a[right] = tmp;
    }


    private void invert(int[] a) {
        for (int i = 0; i < a.length; i++) {
            a[i] = a[i] ^ 1;
        }
    }

    public static void main(String[] args) {
        int[][] A = {{1, 1, 0, 0}, {1, 0, 0, 1}, {0, 1, 1, 1}, {1, 0, 1, 0}};
        FlippingAnImage832 filp = new FlippingAnImage832();
        filp.flipAndInvertImage(A);
        for (int i = 0; i < A.length; i++) {
            ArraysUtils.print(A[i]);
        }
    }
}
